#include "GLTextures.h"

#include <iostream>
#include "sdl.h"
#include "sdl_image.h"
#include "gl.h"

namespace catan
{

GLTextures::GLTextures(GameState &gameState): gameStateRef(&gameState)
{
	if (!gameStateRef->isRunning())
	{
		return;
	}
	create();
}

GLTextures::~GLTextures()
{
	dispose();
}

/*private*/ void GLTextures::createTexture(char* imagePath, GLuint &texture)
{
	SDL_Surface *surface = reinterpret_cast<SDL_Surface*>(IMG_Load(imagePath));
    if (surface)
    {
		glGenTextures(1, &texture);
	    glBindTexture(GL_TEXTURE_2D, texture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
	    SDL_FreeSurface(surface);
	    loadedTextures.push_back(texture);
    }
}

void GLTextures::create()
{
	std::cout << "create" << std::endl;
	GLint maxTextureSize[5];
	glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize);
	
	if (maxTextureSize[0] < 512)
	{
		std::cerr << "Max texture size is under 512, which is the size of our images." << std::endl;
	    gameStateRef->end();
		return;
	}
	
	createTexture("data/sheep.png", sheepTexture);
	createTexture("data/woods.png", woodsTexture);
	createTexture("data/wheat.png", wheatTexture);
	createTexture("data/brick.png", brickTexture);
	createTexture("data/ore.png", oreTexture);
	createTexture("data/desert.png", desertTexture);
	createTexture("data/sea.png", seaTexture);
	createTexture("data/seaQuestionMark.png", generalHarborTexture);
	createTexture("data/seaWood.png", woodHarborTexture);
	createTexture("data/seaSheep.png", sheepHarborTexture);
	createTexture("data/seaWheat.png", wheatHarborTexture);
	createTexture("data/seaBrick.png", brickHarborTexture);
	createTexture("data/seaOre.png", oreHarborTexture);
	createTexture("data/textwhitebg.png", textWithWhiteBackgroundTexture);
	createTexture("data/textblackbg.png", textWithBlackBackgroundTexture);
	createTexture("data/logo.png", logoTexture);
	
	tileTextures[0] = 0;
	tileTextures[1] = 0;
	char tileNumberString[] = "data/numXX.png";
	for (int i=2; i<=12; ++i)
	{
		tileNumberString[8] = '0' + (i/10);
		tileNumberString[9] = '0' + (i%10);
		createTexture(tileNumberString, tileTextures[i]);
	}
	
	createTexture("data/randomize.png", randomizeTexture);
	createTexture("data/startgame.png", startGameTexture);
	createTexture("data/okbutton.png", okButtonTexture);
	createTexture("data/okbuttondisabled.png", okButtonDisabledTexture);
	createTexture("data/buttonBankTrade.png", buttonBankTradeTexture);
	createTexture("data/buttonBankTradeDisabled.png", buttonBankTradeDisabledTexture);
	createTexture("data/buttonBankTradePressed.png", buttonBankTradePressedTexture);
	createTexture("data/buttonBuyCity.png", buttonBuyCityTexture);
	createTexture("data/buttonBuyDevCard.png", buttonBuyDevCardTexture);
	createTexture("data/buttonBuyDevCardDisabled.png", buttonBuyDevCardDisabledTexture);
	createTexture("data/buttonBuyRoad.png", buttonBuyRoadTexture);
	createTexture("data/buttonBuyRoadDisabled.png", buttonBuyRoadDisabledTexture);
	createTexture("data/buttonBuyRoadPressed.png", buttonBuyRoadPressedTexture);
	createTexture("data/buttonBuySettlement.png", buttonBuySettlementTexture);
	createTexture("data/buttonBuySettlementDisabled.png", buttonBuySettlementDisabledTexture);
	createTexture("data/buttonBuySettlementPressed.png", buttonBuySettlementPressedTexture);
	createTexture("data/buttonCityUpgrade.png", buttonCityUpgradeTexture);
	createTexture("data/buttonCityUpgradeDisabled.png", buttonCityUpgradeDisabledTexture);
	createTexture("data/buttonCityUpgradePressed.png", buttonCityUpgradePressedTexture);
	createTexture("data/buttonEndTurn.png", buttonEndTurnTexture);
	createTexture("data/buttonEndTurnDisabled.png", buttonEndTurnDisabledTexture);
	createTexture("data/buttonPlayerTrade.png", buttonPlayerTradeTexture);
	createTexture("data/buttonPlayerTradeDisabled.png", buttonPlayerTradeDisabledTexture);
	createTexture("data/buttonPlayerTradePressed.png", buttonPlayerTradePressedTexture);
	createTexture("data/buttonRollDice.png", buttonRollDiceTexture);
	createTexture("data/buttonRollDiceDisabled.png", buttonRollDiceDisabledTexture);
	createTexture("data/dice.png", diceTexture);
    glBindTexture(GL_TEXTURE_2D, 0);

	isDisposed = false;
}

void GLTextures::dispose()
{
	if (!isDisposed)
	{
		std::cout << "dispose" << std::endl;
		for (std::vector<GLuint>::const_iterator textureIterator = loadedTextures.begin(); textureIterator != loadedTextures.end(); ++textureIterator)
		{
			glDeleteTextures(1, &*textureIterator);
		}
		isDisposed = true;
	}
}

void GLTextures::refresh()
{
	dispose();
	create();
}

GLuint GLTextures::getSheepTexture() const
{
	return sheepTexture;
}

GLuint GLTextures::getWoodsTexture() const
{
	return woodsTexture;
}

GLuint GLTextures::getWheatTexture() const
{
	return wheatTexture;
}

GLuint GLTextures::getBrickTexture() const
{
	return brickTexture;
}

GLuint GLTextures::getOreTexture() const
{
	return oreTexture;
}

GLuint GLTextures::getDesertTexture() const
{
	return desertTexture;
}

GLuint GLTextures::getSeaTexture() const
{
	return seaTexture;
}

GLuint GLTextures::getGeneralHarborTexture() const
{
	return generalHarborTexture;
}

GLuint GLTextures::getWoodHarborTexture() const
{
	return woodHarborTexture;
}

GLuint GLTextures::getSheepHarborTexture() const
{
	return sheepHarborTexture;
}

GLuint GLTextures::getWheatHarborTexture() const
{
	return wheatHarborTexture;
}

GLuint GLTextures::getBrickHarborTexture() const
{
	return brickHarborTexture;
}

GLuint GLTextures::getOreHarborTexture() const
{
	return oreHarborTexture;
}

GLuint GLTextures::getTileTexture(int tileNumber) const
{
	return ((tileNumber >= 2 && tileNumber <= 12) ? tileTextures[tileNumber] : 0);
}

GLuint GLTextures::getOKButtonTexture() const
{
	return okButtonTexture;
}

GLuint GLTextures::getOKButtonDisabledTexture() const
{
	return okButtonDisabledTexture;
}

GLuint GLTextures::getRandomizeTexture() const
{
	return randomizeTexture;
}

GLuint GLTextures::getStartGameTexture() const
{
	return startGameTexture;
}

GLuint GLTextures::getTextWithWhiteBackgroundTexture() const
{
	return textWithWhiteBackgroundTexture;
}

GLuint GLTextures::getTextWithBlackBackgroundTexture() const
{
	return textWithBlackBackgroundTexture;
}

GLuint GLTextures::getLogoTexture() const
{
	return logoTexture;
}

GLuint GLTextures::getButtonBankTradeTexture () const{
	return buttonBankTradeTexture;
}
GLuint GLTextures::getButtonBankTradeDisabledTexture () const{
	return buttonBankTradeDisabledTexture;
}
GLuint GLTextures::getButtonBankTradePressedTexture () const{
	return buttonBankTradePressedTexture;
}
GLuint GLTextures::getButtonBuyCityTexture () const{
	return buttonBuyCityTexture;
}
GLuint GLTextures::getButtonBuyDevCardTexture () const{
	return buttonBuyDevCardTexture;
}
GLuint GLTextures::getButtonBuyDevCardDisabledTexture () const{
	return buttonBuyDevCardDisabledTexture;
}
GLuint GLTextures::getButtonBuyRoadTexture () const{
	return buttonBuyRoadTexture;
}
GLuint GLTextures::getButtonBuyRoadDisabledTexture () const{
	return buttonBuyRoadDisabledTexture;
}
GLuint GLTextures::getButtonBuyRoadPressedTexture () const{
	return buttonBuyRoadPressedTexture;
}
GLuint GLTextures::getButtonBuySettlementTexture () const{
	return buttonBuySettlementTexture;
}
GLuint GLTextures::getButtonBuySettlementDisabledTexture () const{
	return buttonBuySettlementDisabledTexture;
}
GLuint GLTextures::getButtonBuySettlementPressedTexture () const{
	return buttonBuySettlementPressedTexture;
}
GLuint GLTextures::getButtonCityUpgradeTexture () const{
	return buttonCityUpgradeTexture;
}
GLuint GLTextures::getButtonCityUpgradeDisabledTexture () const{
	return buttonCityUpgradeDisabledTexture;
}
GLuint GLTextures::getButtonCityUpgradePressedTexture () const{
	return buttonCityUpgradePressedTexture;
}
GLuint GLTextures::getButtonEndTurnTexture () const{
	return buttonEndTurnTexture;
}
GLuint GLTextures::getButtonEndTurnDisabledTexture () const{
	return buttonEndTurnDisabledTexture;
}
GLuint GLTextures::getButtonPlayerTradeTexture () const{
	return buttonPlayerTradeTexture;
}
GLuint GLTextures::getButtonPlayerTradeDisabledTexture () const{
	return buttonPlayerTradeDisabledTexture;
}
GLuint GLTextures::getButtonPlayerTradePressedTexture () const{
	return buttonPlayerTradePressedTexture;
}
GLuint GLTextures::getButtonRollDiceTexture () const{
	return buttonRollDiceTexture;
}
GLuint GLTextures::getButtonRollDiceDisabledTexture () const{
	return buttonRollDiceDisabledTexture;
}
GLuint GLTextures::getDiceTexture() const{
	return diceTexture;
}
}

